package com.wl.api.service.system.impl;

import static com.wl.api.common.utils.PageInfo.transformPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.PageInfo;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.dto.system.SecurityChannelDomesticDTO;
import com.wl.api.dto.system.SecurityChannelDomesticLadderPriceDTO;
import com.wl.api.mapper.system.SecurityChannelDomesticMapper;
import com.wl.api.model.system.SecurityChannelDomestic;
import com.wl.api.model.system.SecurityChannelDomesticLadderPrice;
import com.wl.api.service.system.ISecurityChannelDomesticLadderPriceService;
import com.wl.api.service.system.ISecurityChannelDomesticService;

/**
 *
 * SecurityChannelDomestic 表数据服务层接口实现类
 *
 */
@Service
public class SecurityChannelDomesticServiceImpl extends ServiceImpl<SecurityChannelDomesticMapper, SecurityChannelDomestic> implements ISecurityChannelDomesticService {

	ISecurityChannelDomesticLadderPriceService domesticLadderPriceService;
	
	@Transactional
	@Override
	public int insertChannelDomesticLadder(SecurityChannelDomesticDTO param) {
		SecurityChannelDomestic entity = BeanUtils.copyProperties(param, SecurityChannelDomestic.class);
		insert(entity);
		List<SecurityChannelDomesticLadderPrice> ladders = param.getLadderPriceList().parallelStream().map(s -> BeanUtils.copyProperties(s, SecurityChannelDomesticLadderPrice.class).buildDomesticId(entity.getId())).collect(Collectors.toList());
		domesticLadderPriceService.insertBatch(ladders);
		return entity.getId();
	}

	@Transactional
	@Override
	public int updateChannelDomesticLadder(SecurityChannelDomesticDTO param) {

		List<SecurityChannelDomesticLadderPrice> source =  domesticLadderPriceService.selectList(new EntityWrapper<SecurityChannelDomesticLadderPrice>().where("domestic_id={0}", param.getId()));
		List<Integer> delIds = new ArrayList<Integer>();
		List<SecurityChannelDomesticLadderPrice> updateList = param.getLadderPriceList().parallelStream().filter(s -> !ObjectUtil.isEmpty(s.getId())).map(s -> BeanUtils.copyProperties(s, SecurityChannelDomesticLadderPrice.class)).collect(Collectors.toList()); 
		List<SecurityChannelDomesticLadderPrice> insertList = param.getLadderPriceList().parallelStream().filter(s -> ObjectUtil.isEmpty(s.getId())).map(s -> BeanUtils.copyProperties(s, SecurityChannelDomesticLadderPrice.class)).collect(Collectors.toList());
		List<Integer> ids = source.parallelStream().map(SecurityChannelDomesticLadderPrice::getId).collect(Collectors.toList());
		//del 
		if (!CollectionUtil.isEmpty(source)) {
			for(SecurityChannelDomesticLadderPrice sc: source) {
				if (CollectionUtil.isEmpty(updateList)) {
					delIds.add(sc.getId());
					continue;
				}
				if (updateList.parallelStream().noneMatch(s -> Objects.equals(s.getId(), sc.getId()))) {
					delIds.add(sc.getId());
					continue;
				}
			}
		}
		updateById(BeanUtils.copyProperties(param, SecurityChannelDomestic.class));
		
		if(!CollectionUtil.isEmpty(delIds)) {
			domesticLadderPriceService.deleteBatchIds(delIds);
		}
		if (!CollectionUtil.isEmpty(updateList)) {
			domesticLadderPriceService.updateBatchById(updateList.parallelStream().filter(s -> ids.contains(s.getId())).collect(Collectors.toList()));
		}
		if (!CollectionUtil.isEmpty(insertList)) {
			domesticLadderPriceService.insertBatch(insertList);
		}
		
		return param.getId();
	}
	
	public SecurityChannelDomesticDTO getChannelDomesticDetail(Integer id) {
		SecurityChannelDomestic detail = selectById(id);
		SecurityChannelDomesticDTO result = BeanUtils.copyProperties(detail, SecurityChannelDomesticDTO.class);
		if (!Objects.isNull(result)) {
			List<SecurityChannelDomesticLadderPrice> list =
					domesticLadderPriceService.selectList(new EntityWrapper<SecurityChannelDomesticLadderPrice>().where("domestic_id={0}", id));
			result.buildLadders(BeanUtils.copyProperties(list, SecurityChannelDomesticLadderPriceDTO.class));
		}
		return result;
	}

	@Override
	public PageInfo<SecurityChannelDomesticDTO> getItemList(SecurityChannelDomesticDTO param) {
		Wrapper<SecurityChannelDomestic> wrapper = new EntityWrapper<SecurityChannelDomestic>().where(" 1=1 ");
		if (!StringUtil.isEmpty(param.getName())) {
			wrapper.like("name", param.getName());
		}
		if (param.getStatus() != null) {
			wrapper.andNewSimple("status",  param.getStatus());
		}
		Page<SecurityChannelDomestic> page = selectPage(new Page<>(param.getPage(), param.getPageSize(), "update_time", false), wrapper);
		
		PageInfo<SecurityChannelDomesticDTO> pageInfo = transformPage(SecurityChannelDomesticDTO.class, page);
		
		if (page!=null && !CollectionUtil.isEmpty(page.getRecords())) {
			for (SecurityChannelDomesticDTO dd: pageInfo.getItems()) {
				dd.setStatusName((dd.getStatus() == null || dd.getStatus()==0) ? "待上线":(dd.getStatus() == 1? "已发布":"下架"));
			}
		}
		return  pageInfo;
	}
	
}